﻿using System.Collections.Generic;
using WCell.RealmServer.Global;
using WCell.RealmServer.Guilds;
using WCell.RealmServer.Entities;
using WCell.RealmServer.Factions;

namespace Kingdoms
{
    public abstract class KingdomGuard : NPC
    {
        public uint GuildID
        {
            get;
            set;
        }

        public int CountLevel
        {
            get;
            set;
        }

        public int TrainingLevel
        {
            get;
            set;
        }
        
        public int ExperienceLevel
        {
            get;
            set;
        }

        public int TerritoryID
        {
            get;
            set;
        }


        public NPCDatabaseEntry dbentry = null;
        
        
        /// <summary>
        /// Override this in individual guard classes to set stats, hp, whatever. REMEMBER to call base.
        /// </summary>
        public virtual void ChangeExperienceLevel(int level)
        {
            ExperienceLevel = level;
        }
        
        private bool IsFriend(IFactionMember opponent)
        {
            if (opponent == this || (opponent is Unit && ((Unit)opponent).Master == this))
                return true;

            if (GuildID == 0)
                return true;

            else if (opponent is KingdomGuard)
            {
                var guard = opponent as KingdomGuard;
                if (guard.GuildID == this.GuildID)
                    return true;
                else return false;
            }
            else if (opponent is Character)
            {
                var player = opponent as Character;
                if (player.GuildId == this.GuildID)
                    return true;
                else return false; //We could add guild alliances...
            }
            else if (opponent is Unit)
            {
                var unit = opponent as Unit;
                if (unit.Master is KingdomGuard)
                {
                    var master = unit.Master as KingdomGuard;
                    if (master.GuildID == this.GuildID)
                        return true;
                    else return false;
                }
                else if (unit.Master is Character)
                {
                    var master = unit.Master as Character;
                    if (master.GuildId == this.GuildID)
                        return true;
                }
                else return false;
            }
            return false;

        }

        public override bool IsFriendlyWith(IFactionMember opponent)
        {
            return IsFriend(opponent);
        }

        public override bool IsHostileWith(IFactionMember opponent)
        {
            if (IsFriend(opponent) == true)
                return false;
            else return true;
        }

        public override bool MayAttack(IFactionMember opponent)
        {
            if (IsFriend(opponent) == true)
                return false;
            else return true;
        }

        public override bool IsAlliedWith(IFactionMember opponent)
        {
            return IsFriend(opponent);
        }
    }
}
